﻿/*----------------------------------------------------------------
  //Copyright (C) 2012 赵国伟
  //文件功能描述：上传文件管理；
  //创建人：赵国伟
  //生成时间：2012/04/22 16:15:00
//----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;

using Shopping.Common;
using Shopping.Model;
using Shopping.DAL;

namespace Shopping.BLL
{
    public class FilesManage
    {

        #region "移动图片到指定的文件夹； by 赵国伟 2012-05-12"
        /// <summary>
        /// 移动图片到指定的文件夹；
        /// </summary>
        /// <param name="fileUrl"></param>
        /// <param name="newFolder"></param>
        /// <returns></returns>
        public static string MoveFileByUrl(string fileUrl,string newFolder)
        {
            if (string.IsNullOrEmpty(fileUrl))
                return fileUrl;

            //执行图片移动；
            string tmpPath = Config.ShoppingFolderUrl;
            if (!tmpPath.EndsWith("/"))
                tmpPath += "/tmp/";
            else
                tmpPath += "tmp/";

            if (fileUrl.IndexOf(tmpPath) < 0)
                return fileUrl;
            string newPath = Config.ShoppingFolder + "\\"+newFolder+"\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";

            Utility.MoveFileByUrl(fileUrl, newPath);
            return fileUrl.Replace("/tmp/", "/" + newFolder + "/");
        }
        #endregion

        #region "处理品牌图片； by 赵国伟 2012-04-22"
        /// <summary>
        /// 处理品牌图片；
        /// </summary>
        public static void FormatBrandLogo(BrandInfo b)
        {
            if (string.IsNullOrEmpty(b.Brand.Logo))
                return;

            //执行图片移动；
            string tmpPath = Config.ShoppingFolderUrl;
            if (!tmpPath.EndsWith("/"))
                tmpPath += "/tmp/";
            else
                tmpPath += "tmp/";

            if (b.Brand.Logo.IndexOf(tmpPath) < 0)
                return;

            string newPath = Config.ShoppingFolder + "\\Brands\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";

           ReturnValue ret = Utility.MoveFileByUrl(b.Brand.Logo, newPath);
           if (ret.IsSucc)
               b.Brand.Logo = b.Brand.Logo.Replace("/tmp/", "/Brands/");
        }
        #endregion

        #region "处理商品推荐图片； by 赵国伟 2012-08-08"
        /// <summary>
        /// 处理商品推荐图片；
        /// </summary>
        public static void FormatCommendImg(GoodsCommend c)
        {
            if (string.IsNullOrEmpty(c.CommendImg))
                return;

            //执行图片移动；
            string tmpPath = Config.ShoppingFolderUrl;
            if (!tmpPath.EndsWith("/"))
                tmpPath += "/tmp/";
            else
                tmpPath += "tmp/";

            if (c.CommendImg.IndexOf(tmpPath) < 0)
                return;

            string newPath = Config.ShoppingFolder + "\\Commends\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";

            ReturnValue ret = Utility.MoveFileByUrl(c.CommendImg, newPath);
            if (ret.IsSucc)
                c.CommendImg = c.CommendImg.Replace("/tmp/", "/Commends/");
        }
        #endregion

        #region "处理商品图片 by 赵国伟 2012-04-22"
        /// <summary>
        /// 处理商品图片
        /// </summary>
        public static  void FormatGoodsImgs(GoodsInfo g)
        {
            if(g.Pictures == null) return ;

            string tmpPath = Config.ShoppingFolderUrl;
            if (!tmpPath.EndsWith("/"))
                tmpPath += "/tmp/";
            else
                tmpPath += "tmp/";
            string newPath = Config.ShoppingFolder + "\\Goods\\" + DateTime.Now.ToString("yyyy-MM-dd") + "\\";

            foreach (GoodsPictures p in g.Pictures)
            {
                if (p.OriginalPictureUrl.IndexOf(tmpPath) >= 0)
                {
                    //对原图进行移动到指定文件夹下；
                    ReturnValue ret = Utility.MoveFileByUrl(p.OriginalPictureUrl, newPath);
                    if (ret.IsSucc)
                    {
                        p.OriginalPictureUrl = p.OriginalPictureUrl.Replace("/tmp/", "/Goods/");

                        string OriginalFile = HttpContext.Current.Server.MapPath(p.OriginalPictureUrl);
                        //生成超小图；
                        ImageHelper.ResizeImage(OriginalFile, OriginalFile.Insert(OriginalFile.LastIndexOf("."), "_l"), Config.SuperSmallPicture_Width, Config.SuperSmallPicture_Height);
                        p.SuperSmallPictureUrl = p.OriginalPictureUrl.Insert(p.OriginalPictureUrl.IndexOf("."), "_l");

                        //生成小图
                        ImageHelper.ResizeImage(OriginalFile, OriginalFile.Insert(OriginalFile.LastIndexOf("."), "_s"), Config.SmallPicture_Width, Config.SmallPicture_Height);
                        p.SmallPictureUrl = p.OriginalPictureUrl.Insert(p.OriginalPictureUrl.IndexOf("."), "_s");
                        //生成中图，
                        ImageHelper.ResizeImage(OriginalFile, OriginalFile.Insert(OriginalFile.LastIndexOf("."), "_m"), Config.MiddlePicture_Width, Config.MiddlePicture_Height);
                        p.MiddlePictureUrl = p.OriginalPictureUrl.Insert(p.OriginalPictureUrl.IndexOf("."), "_m");
                    }
                   
                }
            }
            

        }
        #endregion

        #region "处理静态页面中上传的图片  by 赵国伟 2012-04-22"
        /// <summary>
        /// 处理静态页面中上传的图片；
        /// </summary>
        public void FormatPagesImgs()
        {

        }
        #endregion

    }
}
